﻿#include "QLabelBase.h"

QCOMMWIDGETS_BEGIN_NAMESPACE

//显示文字构造函数
QGroupLabel::QGroupLabel(const QString text,const QFont ft,QFlags<Qt::AlignmentFlag> alig,const QColor text_color, QWidget *parent)
:QLabel(parent), font(ft), m_pAlignmentFlag(alig)
{
	QPalette pe(this->palette());
	pe.setColor(QPalette::WindowText, text_color);
	this->setPalette(pe);
	this->setFont(ft);
	this->setText(text);
	this->setAlignment(alig);
	m_pEOrientation = (Text);
	//pixmapImage = nullptr;
	// 设置控件不接受焦点
	this->setFocusPolicy(Qt::NoFocus);
}

//显示文字构造函数
QGroupLabel::QGroupLabel(const QString text,const QFont ft,QFlags<Qt::AlignmentFlag> alig,const QColor text_color, bool m_psetWordWrapbool, QWidget *parent)
:QLabel(parent), font(ft), m_pAlignmentFlag(alig)
{
	QPalette pe(this->palette());
	pe.setColor(QPalette::WindowText, text_color);
	this->setPalette(pe);
	this->setFont(ft);
	this->setText(text);
	this->setAlignment(alig);
	m_pEOrientation = (Text);
	this->setWordWrap(m_psetWordWrapbool);
	//pixmapImage = nullptr;
	// 设置控件不接受焦点
	this->setFocusPolicy(Qt::NoFocus);
}

//显示显示单张图构造函数
QGroupLabel::QGroupLabel(QString image, QFlags<Qt::AlignmentFlag> alig, Qt::AspectRatioMode m_pAspectRatioMode, EOrientation m_pEOrientation, QWidget *parent)
:QLabel(parent),m_pAlignmentFlag(alig),m_pAspectRatioMode(m_pAspectRatioMode),m_pEOrientation(m_pEOrientation),strImage(image)
{
	QPixmap pixmapImage;
	pixmapImage.fill(Qt::transparent);
	pixmapImage.load(image);
	
	this->setAlignment(alig);
	if(!pixmapImage.isNull())
	{
		if(m_pEOrientation == Image)
		{
			this->setPixmap(pixmapImage.scaled(width(), height(), m_pAspectRatioMode, m_TransformationMode));// 设置图片填充方式
		}
		else if(m_pEOrientation == Icon)
		{
			this->setPixmap(pixmapImage);// 设置图标
		}
	}
	// 设置控件不接受焦点
	this->setFocusPolicy(Qt::NoFocus);
}

//析构函数
QGroupLabel::~QGroupLabel()
{

    //if(pixmapImage)
    //    delete pixmapImage;
    //pixmapImage = nullptr;
}

//设置显示图片
bool QGroupLabel::SetPixmap(QString image)
{
	if(m_pEOrientation == Text){ return false; }
	if(strImage == image){ return true; }
	
	QPixmap pixmapImage;
	pixmapImage.fill(Qt::transparent);
	pixmapImage.load(image);
	
	
	if(!pixmapImage.isNull())
	{
		strImage = image;
		if(m_pEOrientation == Image)
		{
			this->setPixmap(pixmapImage.scaled(width(), height(), m_pAspectRatioMode, m_TransformationMode));// 设置图片填充方式
		}
		else if(m_pEOrientation == Icon)
		{
			this->setPixmap(pixmapImage);// 设置图标
		}
		return true;
	}
	qDebug("error:image.isNull()");
	return false;
}

// 设置显示图片
bool QGroupLabel::SetPixmap(QPixmap &image)
{
	if(!image.isNull())
	{
		if(m_pEOrientation == Image)
		{
			if(image.width() != width() || image.height() != height()){
				image = image.scaled(width(), height(), m_pAspectRatioMode, m_TransformationMode);
			}
			this->setPixmap(image);// 设置图片填充方式
		}
		else if(m_pEOrientation == Icon)
		{
			this->setPixmap(image);// 设置图标
		}
		return true;
	}
	qDebug("error:image.isNull()");
	return false;

}
// 清除显示图片
void QGroupLabel::clearImg()
{
	strImage = "";
	this->clear();
}

//设置显示文字
void QGroupLabel::SetQStringText(QString text)
{
	if(m_pEOrientation != Text){ return; }
	this->setText(text);
	//this->setFont(font);
	//this->setAlignment(m_pAlignmentFlag);
	
}

//设置文字颜色
void QGroupLabel::SetTextColor(const QColor text_color)
{
	if(m_pEOrientation != Text){ return; }
	QPalette pe(this->palette());
	pe.setColor(QPalette::WindowText, text_color);
	this->setPalette(pe);
}

// 设置选中
void QGroupLabel::SetChecked()
{
	SetTextColor(Qt::yellow);
}

// 移除选中
void QGroupLabel::RemoveChecked()
{
	SetTextColor(Qt::white);
}
	
//窗口尺寸变化自动调用函数
void QGroupLabel::resizeEvent(QResizeEvent *event)
{
	QLabel::resizeEvent(event);
	if(!strImage.isEmpty())
	{
		QPixmap pixmapImage;
		pixmapImage.fill(Qt::transparent);
		pixmapImage.load(strImage);
		
		if(m_pEOrientation == Image)
		{
			this->setPixmap(pixmapImage.scaled(width(), height(), m_pAspectRatioMode, m_TransformationMode));// 设置图片填充方式
		}
		else if(m_pEOrientation == Icon)
		{
			this->setPixmap(pixmapImage);// 设置图标
		}
	}
}

QCOMMWIDGETS_END_NAMESPACE
